package primetest.impl.service;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

import primetest.dto.ConfigDto;
import primetest.impl.dao.ConfigDao;
import primetest.impl.dao.Transaction;
import primetest.impl.service.ConfigService;


public class ConfigService implements Serializable {



	/**
	 *
	 */
	private static final long serialVersionUID = -6628669708513269627L;
	private Transaction transaction;
	private ConfigDao configDao;

	@Inject
	public ConfigService(Transaction transaction,ConfigDao configDao){
		this.transaction = transaction;
		this.configDao = configDao;
	}

	public List<ConfigDto> getConfigList() throws IOException {
		try{
			transaction.begin();
			List<ConfigDto> configList = configDao.getAllList(transaction);
			//transaction.commit();
			return configList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public void addConfig(ConfigDto configDto) throws IOException {
		try{
			transaction.begin();
			configDao.addConfig(transaction, configDto);
			configDao.getAllList(transaction);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void updateConfig(ConfigDto configDto) throws IOException {
		try{
			transaction.begin();
			configDao.updateConfig(transaction, configDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public ConfigDto findConfig(String key) throws IOException {
		try{
			transaction.begin();
			return configDao.findConfig(transaction, key);
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}
}
